package com.heima.comment.service.impl;

import com.heima.comment.config.feign.UserFeign;
import com.heima.comment.dto.ApUser;
import com.heima.comment.dto.CommentRepayLikeDto;
import com.heima.comment.dto.CommentRepaySaveDto;
import com.heima.comment.entity.ApCommentRepay;
import com.heima.comment.entity.ApCommentRepayLike;
import com.heima.comment.service.IApCommentRepayService;
import com.heima.common.aliyun.GreenTextScan;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.util.AppThreadLocalUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.Map;

@Service
public class ApCommentRepayServiceImpl implements IApCommentRepayService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private UserFeign userFeign;


    @Autowired
    private GreenTextScan greenTextScan;

    /**
     * 保存评论回复
     * @param dto
     * @return
     */
    @Override
    public ResponseResult save(CommentRepaySaveDto dto) {

        if (dto==null|| StringUtils.isEmpty(dto.getContent())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        Integer userId = AppThreadLocalUtil.get().getUserId();

        if (userId == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        try {
            Map map = greenTextScan.greenTextScan(dto.getContent());
            String suggestion = (String) map.get("suggestion");
            if(!suggestion.equals("pass"))
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        } catch (Exception e) {
            e.printStackTrace();
        }

        ResponseResult responseResult = userFeign.findById(userId);
        ApUser user = (ApUser) responseResult.getData();

        ApCommentRepay apCommentRepay = new ApCommentRepay();
        BeanUtils.copyProperties(dto, apCommentRepay);
        apCommentRepay.setCreatedTime(new Date());
        apCommentRepay.setCommentName(user.getName());
        apCommentRepay.setCommentRepayId(userId.longValue());
        apCommentRepay.setLikes(0L);

        mongoTemplate.insert(apCommentRepay);
        return ResponseResult.okResult();
    }




    @Override
    public ResponseResult like(CommentRepayLikeDto dto) {

        Integer userId = AppThreadLocalUtil.get().getUserId();

        ApCommentRepayLike commentRepayId1 = mongoTemplate.findOne(new Query(Criteria.where("commentRepayId").is(dto.getCommentRepayId())), ApCommentRepayLike.class);
        if (commentRepayId1.getAuthorId() == userId) {
            commentRepayId1.setOperation(dto.getOperation());
            mongoTemplate.save(commentRepayId1);
        }else {
            //保存点赞记录
            commentRepayId1= new ApCommentRepayLike();
            commentRepayId1.setCommentRepayId(dto.getCommentRepayId());
            commentRepayId1.setOperation(dto.getOperation());
            commentRepayId1.setAuthorId(userId);
            mongoTemplate.save(commentRepayId1);
        }

        if (dto.getOperation() == 1) {
            ApCommentRepay commentRepayId = mongoTemplate.findOne(new Query(Criteria.where("commentRepayId").is(dto.getCommentRepayId())), ApCommentRepay.class);

            Long likes = commentRepayId.getLikes();

            likes = likes + 1;
            commentRepayId.setLikes(likes);

            mongoTemplate.save(commentRepayId);
            return ResponseResult.okResult(likes);
        }
        else {
            ApCommentRepay commentRepayId = mongoTemplate.findOne(new Query(Criteria.where("commentRepayId").is(dto.getCommentRepayId())), ApCommentRepay.class);

            Long likes = commentRepayId.getLikes();

            likes = likes - 1;
            commentRepayId.setLikes(likes);

            mongoTemplate.save(commentRepayId);

            return ResponseResult.okResult(likes);
        }
    }

}
